<?php

class Application_Model_dataDAO_TaiKhoanDAO implements Application_Model_dataDAOInf_TaiKhoanDAOInf {
	/**
	 * @param unknown_type $taikhoan
	 */
	protected static $_instance = null;
	
	protected function __construct() {
	
	}
	public static function getInstance() {
		if (self::$_instance == null) {
			self::$_instance = new self ();
		}
		return self::$_instance;
	}
	
	public function insertTaiKhoa($taikhoan) {
		
		$result =-1;
		if ($taikhoan instanceof Application_Model_bean_TaiKhoan) {
			
			try {
				$db = Application_Model_dataDAO_DBConnect::conn ();
				$dmarray = array ("username" => $taikhoan->getUsername (), 
									"password" => $taikhoan->getPassword (), 
									"hoVaTen" => $taikhoan->getHoVaTen (),
									 "gioiTinh" => $taikhoan->getGioiTinh (), 
									 "cmnd" => $taikhoan->getCmnd (), 
									 "diaChi" => $taikhoan->getDiaChi (),
									 "soDienThoai" => $taikhoan->getSoDienThoai(),
									"email"=>$taikhoan->getEmail(),
									 "nhanThongTinQuaEmail"=>$taikhoan->getNhanThongTinQuaEmail(),
									"quyenMacDinh"=>"khach,user",
									"trangThai"=>$taikhoan->getTrangThai(),
									"biXoaHayChua"=>1						
									);

				$db->insert ( 'tb_taikhoan', $dmarray );
				
				
				
				$statement = "SELECT maTaiKhoan FROM tb_taikhoan Where username='" . $taikhoan->getUsername ()."'";
				$results = $db->fetchRow ( $statement );
				$result=$results["maTaiKhoan"];
				
				$insertarray=array("maTaiKhoan"=>$result);
				$db->insert ( 'tb_quyen', $insertarray );
				
				$db->closeConnection ();
			
			} catch ( Zend_Db_Exception $e ) {
				echo $e->getMessage ();
			}
			return $result;
		
		}
	
	}
	
	/**
	 * 
	 */
	public function editTaiKhoan($taikhoan) {
			$result=false;
		try {
			$db = Application_Model_dataDAO_DBConnect::conn ();

			if ($taikhoan instanceof Application_Model_bean_TaiKhoan) {
				$condition = array ("maTaiKhoan=" . $taikhoan->getMaTaiKhoan());
				$update = array ();
				$password=$taikhoan->getPassword();
				$hoten = $taikhoan->getHoVaTen ();
				$cmnd = $taikhoan->getCmnd ();
				$diachi = $taikhoan->getDiaChi ();
				$sodienthoai=$taikhoan->getSoDienThoai();
				$email=$taikhoan->getEmail();
				
				if ($hoten != NULL && $hoten != "")
					$update ["hoVaTen"] = $hoten;
					
				if ($diachi != NULL && $diachi != "")
					$update ["diaChi"] = $diachi;

				if ($cmnd != NULL && $cmnd != "")
					$update ["cmnd"] = $cmnd;
				
				if ($sodienthoai != NULL && $sodienthoai != "")
					$update ["soDienThoai"] = $sodienthoai;
				
				if ($email != NULL && $email != "")
					$update ["email"] = $email;
					if ($password != NULL && $password != "")
					$update ["password"] = $password;
				
					$result = $db->update ( 'tb_taikhoan', $update, $condition );

				$db->closeConnection ();
			}
		
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
				return $result;
	}
	
	/**
	 * 
	 */
	public function removeTaiKhoan($id) {
		$results=false;
		
		try {
			$db = Application_Model_dataDAO_DBConnect::conn ();
			$condition = array ("maTaiKhoan=" . $id );
			$update = array ("biXoaHayChua" => 0 );
			$results = $db->update ( 'tb_taikhoan', $update, $condition );
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
				return $results;
	}
	
	/**
	 * 
	 */
	public function searchTaiKhoan($id) {
		$db = Application_Model_dataDAO_DBConnect::conn ();
		$taikhoan = new Application_Model_bean_TaiKhoan ();
		
		try {
			$statement = "SELECT * FROM tb_taikhoan WHERE maTaiKhoan=" .$id." AND biXoaHayChua=1";
			$result = $db->fetchRow ( $statement );
			if (count ( $result ) > 0) {
				$taikhoan->setUsername ( $result ['username'] );
				$taikhoan->setPassword ( $result ['password'] );
				$taikhoan->setDiaChi ( $result ['diaChi'] );
				$taikhoan->setCmnd ( $result ['cmnd'] );
				$taikhoan->setHoVaTen ( $result ['hoVaTen'] );
				$taikhoan->setSoDienThoai ( $result ['soDienThoai'] );
				$taikhoan->setEmail ( $result ['email'] );
				$taikhoan->setMaTaiKhoan($id);
			} else
				$taikhoan = NULL;
		}
			catch ( Zend_Db_Exception $e ) {
				$taikhoan = NULL;
				echo $e->getMessage ();
		}
		
		return $taikhoan;
	}
	
	/**
	 * 
	 */
	public function activeTaiKhoan($id) {
		$db = Application_Model_dataDAO_DBConnect::conn ();
		$result=false;
		try {
			$condition = array ("maTaiKhoan=" . $id );
			$update = array ();
			$update ["trangThai"] = 1;
			$result = $db->update ( 'tb_taikhoan', $update, $condition );
			//			$statement="UPDATE tb_taikhoan Set trangThai=4 Where maTaiKhoan=3";
			//			$result=$db->fetchAll($statement);
			
			if($result==1)echo " <h3>Chúc mừng bạn đã kích hoạt tài khoản thành công! <h3>";
			else echo "<h3>Tài khoản của bạn đã được kích hoạt rồi <h3>";
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
			$db->closeConnection ();
		return $result;
	}
	/**
	 * @param unknown_type $username
	 * @param unknown_type $password
	 */
	public function login($username, $password) {
		$db = Application_Model_dataDAO_DBConnect::conn ();
		try {
			$statement = "SELECT maTaiKhoan FROM tb_taikhoan
							WHERE username='" . $username . "' AND password='" . $password . "'".
												" AND trangThai=1 AND biXoaHayChua=1";
			$result = $db->fetchRow ( $statement );
			if (count ( $result ) > 0)
			return $result ['maTaiKhoan'];
		
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		$db->closeConnection ();
				
		return - 1;
	}
	
	/**
	 * @param unknown_type $maquyen
	 */
	public function checkQuyen($id,$maquyen) {
		$db = Application_Model_dataDAO_DBConnect::conn ();
		try {
			
			$statement = "Select * from tb_quyen Where maTaiKhoan=" . $id;
			$result = $db->fetchRow ( $statement );
			if (count ( $result ) > 0) {
//				$dsquyen = $result ['quyenMacDinh'];
				//			$stringtok=strtok($result,'_');
//				$listquyen [] = array ();
//				$listquyen = explode ( ",", $dsquyen );
				
//				while ( true ) {
//					$element = each ( $listquyen );
//					if (! (is_bool ( $element ))) {
				
				if($result[$maquyen]>0){	
					
					return true;
				}else return false;
			}
		} catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
	}
/**
	 * 
	 */
	public function getAllTaiKhoan($active) {
			$db = Application_Model_dataDAO_DBConnect::conn ();
				$dstaikhoan=array();
				try {
				$statement = "Select * from tb_taikhoan where biXoaHayChua=1 and trangThai=".$active;
				$i=0;
				$result=$db->fetchAll($statement);
				foreach ($result as $value) {
				$taikhoan=new Application_Model_bean_TaiKhoan();	
				$taikhoan->setMaTaiKhoan($value['maTaiKhoan']);
				$taikhoan->setUsername($value['username']);
				$taikhoan->setPassword($value['password']);
				$taikhoan->setHoVaTen($value['hoVaTen']);
				$taikhoan->setCmnd($value['cmnd']);
				$taikhoan->setGioiTinh($value['gioiTinh']);
				$taikhoan->setDiaChi($value['diaChi']);
				$taikhoan->setEmail($value['email']);
				$taikhoan->setSoDienThoai($value['soDienThoai']);
				$taikhoan->setTrangThai($value['trangThai']);
				
				
				$dstaikhoan[$i]=$taikhoan;
				$i++;
			}
				
				}catch(Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
			$db->closeConnection ();
			return $dstaikhoan;
			
	}
/**
	 * @param unknown_type $id
	 */
	public function getQuyen($id) {
		$db = Application_Model_dataDAO_DBConnect::conn ();
		$result=array();
		$arrayq=array();
		try{
			$statement="Select * from tb_quyen where maTaiKhoan=".$id;
			$resultset=$db->fetchRow($statement);
			
			if($resultset>0){
//			$quyen=$resultset['quyenMacDinh'];
//			$result = explode ( ",",$quyen );
			$arrayq=array_keys($resultset);
			
			$i=0;$j=0;	
			foreach($arrayq as $ar){
					if($resultset[$ar]==1&&$ar!="maQuyen"&&$ar!="maTaiKhoan"){
						
						$result[$j]=$arrayq[$i];		
						$j++;
					}
					$i++;
				}
				;
			
			}
		}catch(Zend_Db_Exception $e){
			echo $e->getMessage ();
		}
		$db->closeConnection();
		;
		
		return $result;
	}
/**
	 * @param unknown_type $id
	 * @param unknown_type $array
	 */
	public function ganQuyen($id, $array) {
		$db = Application_Model_dataDAO_DBConnect::conn ();
		$quyen="";
		$result=false;
		try{
			
//			$statement="Select quyenMacDinh from tb_taikhoan where maTaiKhoan=".$id;
//			$resultset=$db->fetchRow($statement);
//			
//			if($resultset>0){
//			$quyen=$resultset['quyenMacDinh'];
//				}
//			foreach ($array as $value){
//				$quyen=$quyen.','.$value;
//			}
			$condition = array ("maTaiKhoan=" . $id );
			$testarray=array("k1"=>0,"k2"=>0,"k3"=>0,"k4"=>0,"k5"=>0,"u1"=>0,
									"a1"=>0,"a2"=>0,"a3"=>0,"a4"=>0,
										"a5"=>0,"a6"=>0,"a7"=>0,
											"g1"=>0,"g2"=>0,"g3"=>0,"g4"=>0,
											"g5"=>0,"g6"=>0,"g7"=>0,"g8"=>0,"g9"=>0,"g10"=>0);
				$db->update ( 'tb_Quyen', $testarray, $condition );
			
			
				$update = $array;
				
			$result=$db->update ( 'tb_Quyen', $update, $condition );
			
		}catch(Zend_Db_Exception $e){
			echo $e->getMessage ();
		}
		$db->closeConnection();
			return $result;
		
		
	}
/**
	 * @param unknown_type unknown_type $username
	 */
	public function searchbyusername($username) {
		$db = Application_Model_dataDAO_DBConnect::conn ();
		$taikhoan = new Application_Model_bean_TaiKhoan ();
		
		try {
			$statement = "SELECT * FROM tb_taikhoan Where username='" . $username."'";
			$result = $db->fetchRow ( $statement );
			if (count ( $result ) > 0) {
				$taikhoan->setUsername ( $result ['username'] );
				$taikhoan->setPassword ( $result ['password'] );
				$taikhoan->setDiaChi ( $result ['diaChi'] );
				$taikhoan->setCmnd ( $result ['cmnd'] );
				$taikhoan->setHoVaTen ( $result ['hoVaTen'] );
				$taikhoan->setSoDienThoai ( $result ['soDienThoai'] );
				$taikhoan->setEmail ( $result ['email'] );
			
			} else
				$taikhoan = NULL;
		}
			catch ( Zend_Db_Exception $e ) {
			echo $e->getMessage ();
		}
		
		return $taikhoan;
	
	}

/**
	 * @param unknown_type unknown_type $ten
	 */
	public function searchbyten($ten) {
	$db = Application_Model_dataDAO_DBConnect::conn ();
				$dstaikhoan=array();
				try {
				$statement = "Select * from tb_taikhoan where biXoaHayChua=1 AND hoVaTen='$ten'";
				$i=0;
				$result=$db->fetchAll($statement);
				foreach ($result as $value) {
				$taikhoan=new Application_Model_bean_TaiKhoan();	
				$taikhoan->setMaTaiKhoan($value['maTaiKhoan']);
				$taikhoan->setUsername($value['username']);
				$taikhoan->setPassword($value['password']);
				$taikhoan->setHoVaTen($value['hoVaTen']);
				$taikhoan->setCmnd($value['cmnd']);
				$taikhoan->setGioiTinh($value['gioiTinh']);
				$taikhoan->setDiaChi($value['diaChi']);
				$taikhoan->setEmail($value['email']);
				$taikhoan->setSoDienThoai($value['soDienThoai']);
				$taikhoan->setTrangThai($value['trangThai']);
				
				
				$dstaikhoan[$i]=$taikhoan;
				$i++;
			}
				
				}catch(Zend_Db_Exception $e ) {
					echo $e->getMessage ();
		}
			$db->closeConnection ();
			return $dstaikhoan;
			
	}

/**
	 * @param unknown_type unknown_type $array
	 */
	public function searchhightlevel($array) {
				$db = Application_Model_dataDAO_DBConnect::conn ();	
				$dstaikhoan=array();
				$ten=$array['hoVaTen'];
				$diachi=$array['diaChi'];
				$email=$array['email'];
				try{
				$statement = "Select * from tb_taikhoan where hoVaTen like '%$ten%' AND diaChi like '%$diachi%' AND email like '%$email%' AND biXoaHayChua=1";
				$i=0;
				$result=$db->fetchAll($statement);
				foreach ($result as $value) {
				$taikhoan=new Application_Model_bean_TaiKhoan();	
				$taikhoan->setMaTaiKhoan($value['maTaiKhoan']);
				$taikhoan->setUsername($value['username']);
				$taikhoan->setPassword($value['password']);
				$taikhoan->setHoVaTen($value['hoVaTen']);
				$taikhoan->setCmnd($value['cmnd']);
				$taikhoan->setGioiTinh($value['gioiTinh']);
				$taikhoan->setDiaChi($value['diaChi']);
				$taikhoan->setEmail($value['email']);
				$taikhoan->setSoDienThoai($value['soDienThoai']);
				$taikhoan->setTrangThai($value['trangThai']);
				
				
				$dstaikhoan[$i]=$taikhoan;
				$i++;			
				
				}
				}catch(Zend_Db_Exception $e){
					echo $e->getMessage ();
				}
				
				$db->closeConnection ();
				return $dstaikhoan;
		}
		public function insertTaiKhoanAdmin($taikhoan) {
		
		$result =-1;
		if ($taikhoan instanceof Application_Model_bean_TaiKhoan) {
			
			try {
				$db = Application_Model_dataDAO_DBConnect::conn ();
				$dmarray = array ("username" => $taikhoan->getUsername (), 
									"password" => $taikhoan->getPassword (), 
									"hoVaTen" => $taikhoan->getHoVaTen (),
									 "gioiTinh" => $taikhoan->getGioiTinh (), 
									 "cmnd" => $taikhoan->getCmnd (), 
									 "diaChi" => $taikhoan->getDiaChi (),
									 "soDienThoai" => $taikhoan->getSoDienThoai(),
									"email"=>$taikhoan->getEmail(),
									 "nhanThongTinQuaEmail"=>$taikhoan->getNhanThongTinQuaEmail(),
									"quyenMacDinh"=>"admin",
									"trangThai"=>$taikhoan->getTrangThai(),
									"biXoaHayChua"=>1						
									);

				$db->insert ( 'tb_taikhoan', $dmarray );
				
				
				
				$statement = "SELECT maTaiKhoan FROM tb_taikhoan Where username='" . $taikhoan->getUsername ()."'";
				$results = $db->fetchRow ( $statement );
				$result=$results["maTaiKhoan"];
				
				$insertarray=array("maTaiKhoan"=>$result);
				$db->insert ( 'tb_quyen', $insertarray );
				
				$db->closeConnection ();
			
			} catch ( Zend_Db_Exception $e ) {
				echo $e->getMessage ();
			}
			return $result;
		
		}
	
	}
		public function checkAdmin($id){
				$resultboolean=false;	
				try {
				$db = Application_Model_dataDAO_DBConnect::conn ();	
				$statement = "Select * from tb_taikhoan where biXoaHayChua=1 AND maTaiKhoan=$id AND quyenMacDinh='admin'";
				$i=0;
				$result=$db->fetchRow($statement);
				
				if(count($result)>1){
					$resultboolean=true;
				}
					
				$db->closeConnection ();
				}
				catch (Zend_Db_Exception $e ) {
							echo $e->getMessage ();
					}
		
					return $resultboolean;
		}
}

?>